package org.dream.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import org.dream.domain.Stock;
import org.dream.service.DaydealService;

/**
 * 股票日线列表面板
 * 
 * @author qiuriyuchen E-mail:panpan_001@126.com
 */
public class VolumeTablePanel extends JPanel {
	private boolean DEBUG = false;
	JTextField stockCodeText;
	JTable table = new JTable();
	JComboBox startYearBox = UIHelper.getYearComboBox();
	JComboBox startMonthBox = UIHelper.getMonthComboBox();
	JComboBox endYearBox = UIHelper.getYearComboBox();
	JComboBox endMonthBox = UIHelper.getMonthComboBox();
	JTextField zhangFuField;
	JTextField liangZhangFuField;
	private Vector<String> columnNames = new Vector<String>();

	private Vector<Vector<Object>> data = new Vector<Vector<Object>>();

	public VolumeTablePanel(Stock stock) {

		// super(new GridLayout(1, 0));
		super(new BorderLayout());
		columnNames.add("序号");
		columnNames.add("时间");
		columnNames.add("股票代码");
		columnNames.add("股票名称");
		columnNames.add("收盘价格");
		columnNames.add("成交量");
		columnNames.add("涨幅");
		columnNames.add("成交量涨幅");
		columnNames.add("价量得分");
		columnNames.add("累积得分");
		// 添加查询条件
		addFindPanel(stock.getCode());
		JScrollPane scrollPane = new JScrollPane(table);

		VolumeTablePanel.this.add(scrollPane, BorderLayout.CENTER);
		VolumeTablePanel.this.setVisible(true);
		// 添加日线数据表格面板
		refreshDataTable();

	}

	public VolumeTablePanel(String stockCode) {
		this(Stock.activationDomain(stockCode));
	}

	/**
	 * 添加查询条件
	 */
	private void addFindPanel(String code) {
		stockCodeText = new JTextField(code, 10);
		zhangFuField = new JTextField("1", 10);
		liangZhangFuField = new JTextField("35.0", 10);
		// Add the scroll pane to this panel.
		JPanel panel = new JPanel();

		JButton findButton = new JButton("查询");
		panel.add(new JLabel("股票代码"));
		panel.add(stockCodeText);

		panel.add(new JLabel("开始时间"));

		panel.add(startYearBox);

		panel.add(startMonthBox);
		panel.add(new JLabel("结束时间"));
		endYearBox.setSelectedIndex(10);
		panel.add(endYearBox);
		panel.add(endMonthBox);
		panel.add(new JLabel("涨幅"));
		panel.add(zhangFuField);
		panel.add(new JLabel("增量"));
		panel.add(liangZhangFuField);
		panel.add(findButton);
		add(panel, BorderLayout.NORTH);
		findButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				refreshDataTable();

			}
		});
	}

	private String getStartDate() {
		String str = ("" + startYearBox.getSelectedItem() + startMonthBox.getSelectedItem()).replaceAll("年", "/");
		str = str.replaceAll("月", "/");
		return str;
	}

	private String getEndDate() {
		String str = ("" + endYearBox.getSelectedItem() + endMonthBox.getSelectedItem()).replaceAll("年", "/");
		str = str.replaceAll("月", "/");
		return str;
	}

	/**
	 * 添加日线数据表格
	 */
	private void refreshDataTable() {
		data = new DaydealService().getDaydealTurnoverTable(stockCodeText.getText(), getStartDate(), getEndDate(),
				Double.parseDouble(zhangFuField.getText()), Double.parseDouble(liangZhangFuField.getText()));

		TableModel dataModel = new DefaultTableModel(data, columnNames);
		table.setModel(dataModel);

	}
}
